package com.jiayang.hhotel.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.ReservationSearchAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.ReservationSearchModel;
import com.jiayang.hhotel.presenter.ReservationSearchPresenter;
import com.jiayang.hhotel.ui.ReservationSearchDetailsActivity;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XLazyFragment;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 订房查询子fragment 传递参数用于判断到底是来自那种item的请求
 */

public class ReservationSearchFragment extends XLazyFragment<ReservationSearchPresenter> {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private SwipeRefreshLayout fresh;
    private SwipeMenuRecyclerView recyclerView;
    private int flag;
    private String startDate="",endDate="",searchData = "";//开始/结束日期/搜索框中的内容

    private ReservationSearchAdapter adapter;

    private int page = 1;//此处是分页的页数，暂时没有添加，需要添加时只需要在请求中添加page字段即可

    public ReservationSearchFragment(){

    }

    public  ReservationSearchFragment(int flag){
        this.flag=flag;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        layout = getRootView().findViewById(R.id.ll_data);
        icon = (ImageView) getRootView().findViewById(R.id.iv_pic);
        tv = (TextView) getRootView().findViewById(R.id.tv_text);

        fresh= (SwipeRefreshLayout) getRootView().findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) getRootView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        isLoadMore = true;
        isRefresh(true);
        initRequest();
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isLoadMore){
                    page = 1;
                    initRequest();
                }else{
                    isRefresh(false);
                }
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    // TODO 这里有个注意的地方，如果你刚进来时没有数据，但是设置了适配器，这个时候就会触发加载更多，需要开发者判断下是否有数据，如果有数据才去加载更多。
                    if(!isLoadMore){
                        isLoadMore = true;
                        page++;
                        initRequest();
                    }
                }
            }
        });
    }

    /**
     *
     * 设置开始和结束日期
     * @param startDate 开始日期
     * @param endDate 结束日期
     */
    public void setDate(String startDate,String endDate){
        this.startDate = startDate;
        this.endDate = endDate;
        fresh.setVisibility(View.VISIBLE);
        layout.setVisibility(View.GONE);
        isRefresh(true);
        initRequest();
        isLoadMore = true;
    }

    /**
     *
     * 设置搜索框中的内容
     * @param data 文字
     */
    public void setSearchData(String data){
        searchData = data;
        fresh.setVisibility(View.VISIBLE);
        layout.setVisibility(View.GONE);
        isRefresh(true);
        initRequest();
        isLoadMore = true;
    }

    //初始请求，判断请求哪种状态的数据
    private void initRequest(){
        switch (flag){
            case 0://全部
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,0,0,0,0,searchData,page);
                break;
            case 1://散客
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,1,0,0,0,searchData,page);
                break;
            case 2://团队
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,2,0,0,0,searchData,page);
                break;
            case 3://旅行社
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,3,0,0,0,searchData,page);
                break;
            case 4://明日到
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,getP().getTomorrowDate(),endDate,0,3,0,0,0,searchData,page);
                break;
            case 5://有保
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,0,1,0,0,searchData,page);
                break;
            case 6://无保
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,0,2,0,0,searchData,page);
                break;
            case 7://接机
                getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,startDate,endDate,0,0,0,1,0,searchData,page);
                break;
        }
    }

    private void isRefresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    public void setAdapter(List<ReservationSearchModel.PRsvbillRoomListDtosBean> model){
        isRefresh(false);
        if(adapter==null){
            adapter=new ReservationSearchAdapter(getActivity(), model, new ReservationSearchAdapter.OnItemClickListener() {
                @Override
                public void ItemClickListener(ReservationSearchModel.PRsvbillRoomListDtosBean model, int position) {
                    Intent intent=new Intent(getActivity(), ReservationSearchDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constant.INTENT_PROJECT_KEY,model);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }
    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isRefresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            fresh.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getActivity().getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getActivity().getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fresh.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isRefresh(true);
                            initRequest();
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getActivity().getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
        }else{//之前请求到有数据
            ToastUtil.Toast(StringUtil.errMsg(state),getActivity());
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_reservation_search_item;
    }

    @Override
    public ReservationSearchPresenter newP() {
        return new ReservationSearchPresenter();
    }
}
